
package es.upm.dit.gsi.opactv.searchengines;

import es.upm.dit.gsi.opactv.util.AwsHandlerResolver;
import es.upm.dit.gsi.opactv.Book;
import com.ECS.client.jax.AWSECommerceService;
import com.ECS.client.jax.AWSECommerceServicePortType;
import com.ECS.client.jax.Image;
import com.ECS.client.jax.Item;
import com.ECS.client.jax.ItemSearch;
import com.ECS.client.jax.ItemSearchRequest;
import com.ECS.client.jax.Items;
import com.ECS.client.jax.OperationRequest;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.Holder;

public class Amazon implements SearchEngine {

    private final String accessKey = "AKIAJ3R2QG6K6IHULALQ";
    private final String secretKey = "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";
    private String title, author, tags;
    private int maxPages = 1;

    public Amazon() {
        // COLLECTION!
    }

    public void set(String title, String author, String tags) {
        this.title = title;
        this.author = author;
        this.tags = tags;
    }

    public ArrayList<Book> getBooks() {
        ArrayList<Book> books = new ArrayList();
        for (Items items : getAllBooks()) {
            for (Item item : items.getItem()) {
                String bookTitle,bookAuthor="";
                bookTitle = item.getItemAttributes().getTitle();
                List<String> authors = item.getItemAttributes().getAuthor();
                for (int i=0; i<authors.size(); i++) {
                    if (i>0) bookAuthor += ", ";
                    bookAuthor += authors.get(i);
                }
                Book book = new Book(bookTitle, bookAuthor);
                book.addOptional("AMAZON:ASIN", item.getASIN());
                books.add(book);
            }
        }
        return books;
    }

    public void completeBook(Book book) {
        String asin = book.getOptional("AMAZON:ASIN");
        if (!asin.equals("")) book.addCover(getCover(asin));
    }

    private String getCover(String asin) {
        Item item = itemSearchFromASIN(asin).getItem().get(0);
        Image cover = item.getLargeImage();
        if (cover!=null) {
            String coverURL = cover.getURL();
            if (coverURL!=null) return coverURL;
        }
        return "";
    }

    private ArrayList<Items> getAllBooks() {
        ArrayList<Items> itemsList = new ArrayList();
        itemsList.add(itemSearch("1"));
        int pages = itemsList.get(0).getTotalPages().intValue();
        if (pages>maxPages) pages = maxPages;
        for (int i=2; i<=pages; i++) {
            String page = String.valueOf(i);
            itemsList.add(itemSearch(page));
        }
        return itemsList;
    }

    private Items itemSearch(String page) {
        AWSECommerceService ecs = new AWSECommerceService();
        ecs.setHandlerResolver(new AwsHandlerResolver(secretKey));
        ItemSearch iss = new ItemSearch();
        iss.setAWSAccessKeyId(accessKey);
        ItemSearchRequest isr = new ItemSearchRequest();
        isr.setTitle(title);
        isr.setAuthor(author);
        isr.setKeywords(tags);
        isr.setSearchIndex("Books");
        isr.setItemPage(new BigInteger(page));
        iss.getRequest().add(isr);
        List<String> responseGroup = isr.getResponseGroup();
        responseGroup.add("ItemAttributes");
        AWSECommerceServicePortType port = ecs.getAWSECommerceServicePort();
        Holder<OperationRequest> opRequest = new Holder<OperationRequest>();
        Holder<List<Items>> items = new Holder<List<Items>>();
        port.itemSearch("", accessKey, accessKey, "", "", "", isr,
                        iss.getRequest(), opRequest, items);
        return items.value.get(0);
    }

    private Items itemSearchFromASIN(String asin) {
        AWSECommerceService ecs = new AWSECommerceService();
        ecs.setHandlerResolver(new AwsHandlerResolver(secretKey));
        ItemSearch iss = new ItemSearch();
        iss.setAWSAccessKeyId(accessKey);
        ItemSearchRequest isr = new ItemSearchRequest();
        isr.setKeywords(asin);
        isr.setSearchIndex("Books");
        iss.getRequest().add(isr);
        List<String> responseGroup = isr.getResponseGroup();
        responseGroup.add("Images");
        AWSECommerceServicePortType port = ecs.getAWSECommerceServicePort();
        Holder<OperationRequest> opRequest = new Holder<OperationRequest>();
        Holder<List<Items>> items = new Holder<List<Items>>();
        port.itemSearch("", accessKey, accessKey, "", "", "", isr,
                        iss.getRequest(), opRequest, items);
        return items.value.get(0);
    }

}
